<template>
	<div>
		<div class="div-top">
			<span>专业管理</span>
		</div>
		<el-button type="primary" style="margin:0 0 10px 10px" @click="openAddRouter()" icon="el-icon-circle-plus-outline">添加专业</el-button>
		<div style="margin: 10px;">
			<el-table :data="tableRouter" style="width: 100%" border>
				<el-table-column prop="name" label="专业名称" align="center">
				</el-table-column>
				<el-table-column label="操作" align="center">
					<template slot-scope="scope">
						<el-button type="primary" @click="openEditRouter(scope.row)" size="small">编辑</el-button>
						<el-button type="danger" @click="removeRouter(scope.row.id)" size="small">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<el-dialog title="添加专业" :close-on-press-escape='false' :close-on-click-modal='false' :append-to-body='true' :visible.sync="majorSaveDialog" width="350px">
			<el-form :model="addForm" :rules="rules" ref="addForm">
				<el-form-item label="专业名称" prop="name">
					<el-input v-model="addForm.name" placeholder="请输入专业名称" maxlength="50" clearable>
					</el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="majorSaveReset">取 消</el-button>
				<el-button type="primary" @click="toAddRouter">确 定</el-button>
			</div>
		</el-dialog>
		<el-dialog title="编辑专业" :close-on-press-escape='false' :close-on-click-modal='false' :append-to-body='true' :visible.sync="editRouterDialog" width="350px">
			<el-form :model="editForm" :rules="rules" ref="editForm">
				<el-form-item label="专业名称" prop="name">
					<el-input v-model="editForm.name" placeholder="请输入专业名称" maxlength="50" clearable>
					</el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="editRouterReset">取 消</el-button>
				<el-button type="primary" @click="editRouter">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
	import { majorList, majorSave, majorUpdate, majorRemove } from '@/api/major.js'
	export default {
		data() {
			return {
				tableRouter: [],
				majorSaveDialog: false,
				editRouterDialog: false,
				addForm: {
					name: '',
				},
				editForm: {
					id: '',
					name: '',
				},
				rules: {
					name: [{
						required: true,
						message: '请输入专业名称'
					}],
				},
			}
		},
		created() {
			this.getRouterList();
		},
		methods: {
			getRouterList() {
				majorList({}).then(res => {
					this.tableRouter = res.data
				})
			},
			openAddRouter() {
				this.majorSaveDialog = true;
			},
			toAddRouter() {
				this.$refs.addForm.validate((valid) => {
					if(valid) {
						majorSave({
							name: this.addForm.name,
						}).then(async res => {
							if(res.code == '200') {
								this.$message({
									message: '添加成功',
									type: 'success'
								});
								this.majorSaveReset();
								this.getRouterList();
							} else {
								this.$message({
									message: res.message,
									type: 'error'
								});
							}
						})
					}
				})
			},
			majorSaveReset() {
				this.$refs.addForm.resetFields();
				this.$refs.addForm.clearValidate();
				this.majorSaveDialog = false;
			},
			openEditRouter(item) {
				this.editForm.id = item.id;
				this.editForm.name = item.name;
				this.editRouterDialog = true;
			},
			editRouter() {
				this.$refs.editForm.validate((valid) => {
					if(valid) {
						majorUpdate({
							name: this.editForm.name,
							id: this.editForm.id
						}).then(async res => {
							if(res.code == '200') {
								this.$message({
									message: '编辑成功',
									type: 'success'
								});
								this.getRouterList();
								this.editRouterReset();
							} else {
								this.$message({
									message: res.message,
									type: 'error'
								});
							}
						})
					}
				})
			},
			editRouterReset() {
				this.editRouterDialog = false;
				this.$refs.editForm.resetFields();
				this.$refs.editForm.clearValidate()
			},
			removeRouter(id) {
				this.$confirm('是否删除该专业?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					majorRemove({
						id: id
					}).then(async res => {
						if(res.code == '200') {
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getRouterList();
						} else {
							this.$message({
								message: res.message,
								type: 'error'
							});
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			}
		}
	}
</script>